package Graph.graphWithNoweight.dfs;

import java.util.ArrayList;

/**
 * @Author: 蔡翔
 * @Date: 2020/2/4 12:39
 * @Version 1.0
 * 2
 */
public class GraphDFS {
    private Graph G;
    private boolean[] visited;

    private ArrayList<Integer> order = new ArrayList<>();

    public GraphDFS(Graph G){
        this.G = G;
        visited = new boolean[G.V()];
        for(int v=0;v<G.V();v++){
            if(!visited[v]){
                dfs(v);
            }
        }
    }

    private void dfs(int v) {
        visited[v] = true;
        order.add(v);
        for(int w:G.adj(v)){
            if(!visited[w]){
                dfs(w);
            }
        }
    }

    public Iterable<Integer> order(){
        return order;
    }

    public static void main(String[] args) {
        Graph g = new Graph("g.txt");
        GraphDFS graphDFS = new GraphDFS(g);
        System.out.println(graphDFS.order());
    }
}
